﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Http;
using System.Web.Mvc;
using DSAServer.Models;
using DSAServer.Models.Input;
using DSAServer.Models.Output;
using DSAServer.Service;
using DSAServer.Utils;
using System.Collections.Specialized;
using System.Text;
using System.IO;

namespace DSAServer.Controllers
{
    public class ManagerController : BaseController
    {

        protected override void OnActionExecuting(System.Web.Mvc.ActionExecutingContext filterContext)
        {
            //检测用户是否正确
            if (filterContext.ActionDescriptor.ActionName != "Login" && filterContext.ActionDescriptor.ActionName != "QueryPhotosize")
            {
                UserLoginInfo loginInfo = new UserLoginInfo();
                loginInfo.user = this.Request["u"];
                loginInfo.password = this.Request["p"];
                var result = new ManagerService().Login(loginInfo);
                if (!result.ok)
                {
                    filterContext.Result = new JsonResult() { Data = new InvokeResult() { msg = result.msg }, JsonRequestBehavior = JsonRequestBehavior.AllowGet };
                    return;
                }
                AppContext.Instance.userId = result.data;
            }

            base.OnActionExecuting(filterContext);
        }

        [System.Web.Http.HttpPost]
        [System.Web.Http.HttpGet]
        public ActionResult Index()
        {

            return Content("");
        }

        public ActionResult Login()
        {
            UserLoginInfo data = getRequestData<UserLoginInfo>();
            var result = new ManagerService().Login(data);
            return Content(JsonHelper.SerializeObject(result));
            //return Json(result, JsonRequestBehavior.AllowGet);
        }

        public ActionResult ChangePassword()
        {
            var data = getRequestData<ChgPwdInfo>();
            var result = new ManagerService().ChangePassword(data);
            return Content(JsonHelper.SerializeObject(result));
            //return Json(result, JsonRequestBehavior.AllowGet);
        }

        public ActionResult QueryUser()
        {
            //var data = getRequestData<UserInfo>();
            var result = new ManagerService().QueryUser();
            return Content(JsonHelper.SerializeObject(result));
        }

        public ActionResult SaveUser()
        {
            var data = getRequestData<UserInfo>();
            var result = new ManagerService().SaveUser(data);
            return Content(JsonHelper.SerializeObject(result));
        }

        public ActionResult LoadConfigs()
        {
            var result = new ManagerService().LoadConfig();
            return Content(JsonHelper.SerializeObject(result));
            //return Json(result, JsonRequestBehavior.AllowGet);
        }

        public ActionResult SaveConfigs()
        {
            var data = getRequestData<List<ConfigItem>>();
            var result = new ManagerService().SaveConfigs(data);
            return Content(JsonHelper.SerializeObject(result));
            //return Json(result, JsonRequestBehavior.AllowGet);
        }

        public ActionResult QueryPhotosize()
        {
            var result = new ManagerService().QueryPhotoSize();
            return Content(JsonHelper.SerializeObject(result));
            //return Json(result, JsonRequestBehavior.AllowGet);
        }

        public ActionResult SavePhotosize()
        {
            var data = getRequestData<PhotoSizeSubmitInfo>();
            var result = new ManagerService().SavePhotoSize(data);
            return Content(JsonHelper.SerializeObject(result));
            //return Json(result, JsonRequestBehavior.AllowGet);
        }

        public ActionResult AddOrder()
        {
            var data = getRequestData<TaobaoOrderInfo>();
            var result = new OrderService().Insert(data);
            return Content(JsonHelper.SerializeObject(result));
            //return Json(result, JsonRequestBehavior.AllowGet);
        }

        //http://localhost:65205/api/order/get?data={%22no%22:%221%22,%22ww%22:%22111%22}&json=true
        public InvokeResult Get()
        {
            InvokeResult result = new InvokeResult();
            TaobaoOrderGetPara data = getRequestData<TaobaoOrderGetPara>();
            result.msg = "test!!!";
            return result;
        }

        public ActionResult QueryUnDownOrders()
        {
            var result = new OrderService().QueryUnDownlaodOrders(getRequestData<PageRequest>());
            LogUtil.Info("查询未下载订单数据" + JsonHelper.SerializeObject(result));
            return Content(JsonHelper.SerializeObject(result));
            //return Json(result, JsonRequestBehavior.AllowGet);
        }

        public ActionResult QueryHasDownOrders()
        {
            var result = new OrderService().QueryHasDownlaodOrders(getRequestData<PageRequest>());
            return Content(JsonHelper.SerializeObject(result));
            //return Json(result, JsonRequestBehavior.AllowGet);
        }

        public ActionResult SetOrderHasDown()
        {
            var data = getRequestData<List<string>>();
            var result = new OrderService().SetOrderHasDown(data);
            return Content(JsonHelper.SerializeObject(result));
            //return Json(result, JsonRequestBehavior.AllowGet);
        }

        public ActionResult SetOrderHasSend()
        {
            var data = getRequestData<List<string>>();
            var result = new OrderService().SetOrderHasSend(data);
            return Content(JsonHelper.SerializeObject(result));
            //return Json(result, JsonRequestBehavior.AllowGet);
        }

        public ActionResult CheckSalesmanExist()
        {
            var data = getRequestData<SalesmanInfo>();
            var result = new ManagerService().CheckSalesmanExist(data);
            return Content(JsonHelper.SerializeObject(result));
        }

        public ActionResult UpdateSalesmanInfo()
        {
            var data = getRequestData<SalesmanInfo>();
            var result = new ManagerService().UpdateSalesmanInfo(data);
            return Content(JsonHelper.SerializeObject(result));
        }

        public ActionResult InsertSalesmanInfo()
        {
            var data = getRequestData<SalesmanInfo>();
            var result = new ManagerService().InsertSalesmanInfo(data);
            return Content(JsonHelper.SerializeObject(result));
        }

        public ActionResult QuerySalesmanInfo()
        {
            //var data = getRequestData<string>();
            var result = new ManagerService().QuerySalesmanInfo();
            return Content(JsonHelper.SerializeObject(result));
        }

        public ActionResult QueryRecommendInfo()
        {
            var data = getRequestData<PageRequest>();
            var result = new ManagerService().QueryRecommendInfo(data);
            return Content(JsonHelper.SerializeObject(result));
        }

        public ActionResult QueryUnSubscribeInfo()
        {
            var data = getRequestData<PageRequest>();
            var result = new ManagerService().QueryUnSubscribeInfo(data);
            return Content(JsonHelper.SerializeObject(result));
        }
    }
}